package com.itheima.service.Impl;

import com.alibaba.fastjson.JSON;
import com.itheima.domain.Product;
import com.itheima.mapper.ProductMapper;
import com.itheima.service.ProductService;
import org.apache.dubbo.config.annotation.DubboService;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@DubboService
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private RestHighLevelClient client;
    /**
     * 搜索商品信息
     * @param searchString 搜索条件
     * @return 搜索结果
     */
    @Override
    public List<Product> search(String searchString) {
        List<Product> list = null;
        try {
            SearchRequest searchRequest = new SearchRequest("j_product");
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(QueryBuilders.matchQuery("title",searchString))
                    .highlighter(new HighlightBuilder().field("title").preTags("<font color='red'>").postTags("</font>"))
                    .from(0)
                    .size(100);
            searchRequest.source(searchSourceBuilder);
            SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
            SearchHit[] hits = response.getHits().getHits();
            list = Arrays.stream(hits).map(hit -> {
                Product product = JSON.parseObject(hit.getSourceAsString(), Product.class);
                product.setTitle(hit.getHighlightFields().get("title").getFragments()[0].string());
                return product;
            }).collect(Collectors.toList());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 添加商品信息
     * @param product 商品对象
     */
    @Override
    public void add(Product product) {
        productMapper.add(product);
        try {
            IndexRequest indexRequest=new IndexRequest("j_product");
            indexRequest.id(String.valueOf(product.getId())).source(JSON.toJSONString(product), XContentType.JSON);
            IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
            System.out.println(response.status());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Product> findAll() {
        return productMapper.findAll();
    }
}
